package com.handy.plugin.auth.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.plugin.auth.bean.HAuthTree;
import com.handy.plugin.tree.TreeBaseBean;
import com.handy.plugin.tree.TreeBaseDAO;
public class HAuthTreeDAO extends TreeBaseDAO {
	
	public HAuthTreeDAO(){
		setTablename(tablename);
	}
	
	/**
	 * ��ȡ���б�
	 * @param roleid
	 * @param userid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<HAuthTree> findAll(Integer roleid,Integer userid){
		StringBuffer sb = new StringBuffer();
		if (roleid<3){//
			sb.append("select * from ").append(tablename).append(" where state!=1 order by oid");
		}else{
			sb.append("select * from ").append(v_tree).append(" where state!=1 ");
			sb.append("and (roleid=").append(roleid).append(" or ").append("userid=").append(userid).append(") ");
			sb.append("order by oid");
		}
		QueryRunner run = new QueryRunner(dbUtil.dataSource(getDataSourceName()));
		BeanListHandler h = new BeanListHandler(HAuthTree.class);
		List<HAuthTree> list = null;
		try {
			list = (List<HAuthTree>)run.query(sb.toString(), h);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return list;
	}
	
	/**
	 * �ɱ���ҵ���Ӧ�Ľڵ�
	 * @param id
	 * @return
	 */
	public HAuthTree findTreeById(Integer id){
		QueryRunner run = new QueryRunner(dbUtil.dataSource(getDataSourceName()));
		StringBuffer sb = new StringBuffer();

		sb.append("select * from ").append(getTablename()).append(" ");
		sb.append("where id=").append(id);

		BeanHandler h = new BeanHandler(HAuthTree.class);

		HAuthTree _bean = null;
		try {
			_bean = (HAuthTree) run.query(sb.toString(), h);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
			
		return _bean;
	}
	
	public HAuthTree insert(HAuthTree bean) {
		if (null == bean){
			log.error("找不到HAuthTree节点信息");
			return null;
		}
		TreeBaseBean baseBean = null;
		try {
			baseBean = insert(bean.getPid());
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		if (null != baseBean){
			bean.setAlevel(baseBean.getAlevel());
			bean.setHavecid(baseBean.getHavecid());
			bean.setOid(baseBean.getOid());
		}else{
			return null;
		}

		StringBuffer sb = new StringBuffer();
		sb.append("insert into ").append(tablename).append(" (").append("id,pid,havecid,oid,alevel,title,kind,note,state").append(")");
		sb.append("values (");
		sb.append("h_auth_tree_seq.nextval,");
		sb.append(bean.getPid()).append(",");
		sb.append(0).append(",");
		sb.append(bean.getOid()).append(",");
		sb.append(bean.getAlevel()).append(",");
		sb.append("'").append(bean.getTitle()).append("',");
		sb.append(bean.getKind()).append(",");
		sb.append("'").append(bean.getNote()).append("',");
		sb.append(0);
		sb.append(")");
		if (log.isDebugEnabled()){
			log.info(sb.toString());
		}
		QueryRunner run = new QueryRunner(dbUtil.dataSource(getDataSourceName()));
		synchronized (KEY) {
			try {
				run.update(sb.toString());
				sb = new StringBuffer();
				sb.append("select * from ").append(tablename).append(" where id = (select max(id) from ");
				sb.append(tablename).append(")");
				BeanHandler h = new BeanHandler(HAuthTree.class);
				try {
					bean = (HAuthTree) run.query(sb.toString(), h);
				} catch (SQLException e) {
					log.error(e.getMessage());
				}
			} catch (SQLException e) {
				bean = null;
				log.error(e.getMessage());
			}
		}
		
		
		return bean;
	}
	
	public HAuthTree update(HAuthTree bean) {
		if (bean == null){
			log.error("找不到bean");
		}
		StringBuffer sb = new StringBuffer();
		sb.append("update ").append(tablename).append(" set ");
		sb.append("title=").append("'").append(bean.getTitle()).append("',");
		sb.append("kind=").append("'").append(bean.getKind()).append("',");
		sb.append("note=").append("'").append(bean.getNote()).append("' ");
		sb.append("where id=").append(bean.getId());
		//log.info(sb.toString());
		QueryRunner run = new QueryRunner(dbUtil.dataSource(getDataSourceName()));
		try {
			run.update(sb.toString());
		} catch (SQLException e) {
			bean = null;
			log.error(e.getMessage());
		}
		return bean;
	}
	
	private static final Log log = LogFactory.getLog(HAuthTreeDAO.class);
	private static final String tablename="h_auth_trees";
	private static final String v_tree = "v_auth_trees";
	private static final byte[] KEY = new byte[0];
}
